/*******************************************************************************
 * Package: com.chris.gateway.filters
 * Type:    AuthGlobalFilter
 * Date:    2024/3/7 下午5:20
 *
 * Copyright (c) 2024 BOING CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.chris.gateway.filters;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * TODO your comment
 *
 * @author Hudesheng
 * @date 2024/3/7 下午5:20
 */
@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if(StringUtils.isBlank(token)){
            System.out.println("鉴权失败！");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        //继续下一个过滤器
        return chain.filter(exchange);
    }
    
    /**
     * 全局过滤器优先级
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}